package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Region;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

@RestController
@RequestMapping("/system/region")
public class RegionManagerController {

    @Autowired
    private RegionService regionService;

    @GetMapping(value = "/item/{id}")
    public AjaxResult getItem(@PathVariable String id) {
        return AjaxResult.success(regionService.Regionlist(id));
    }


    @GetMapping("/treeselect")
    public AjaxResult treeselect(Region region) {
        region.setDeptId("1491669179554595370");
        List<Region> regions = regionService.selectRegionList(region);
        return AjaxResult.success(regionService.buildDeptTreeSelect(regions));
    }
    @Autowired
    private ISysUserService userService;

    /**
     * 一键批量创建用户
     */
    @GetMapping("/oneClickUserCreation")
    public AjaxResult oneClickUserCreation() {
        String trye = "";
        //核对成功次数
        int num = 0;
        //获取菜单列表
        Region region = new Region();
        region.setDeptId("1491669179554595370");
        List<Region> regions = regionService.selectRegionList(region);
        //1.循环所有菜单
        try {
            for (int a = 0; a < regions.size(); a++) {
                if (num == a) {
                    //创建用户
                    SysUser user = new SysUser();
                    //添加用户必要信息
                    user.setUserName(regions.get(a).getName());
                    user.setNickName(regions.get(a).getName());
                    user.setStatus("0");
                    user.setDeptId(regions.get(a).getDeptId());
                    user.setPassword("123456");
                    //校验用户是否存在
                    if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) {
                        num++;
                        continue;
                    } else {
                        Long[] x = {100L};
                        user.setRoleIds(x);
                        user.setCreateBy("admin");
                        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
                        if (UserConstants.UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) {
                            userService.insertUser(user);
                            num++;
                            continue;
                        }
                    }
                }
            }
        } catch (Exception e) {
            trye += e + "报错了";
        }
        System.out.println("报错了---" + trye);
        if (num == regions.size()) {
            return AjaxResult.success(regions.size() + "：拿到了这些用户");
        } else {
            return AjaxResult.error(HttpStatus.BAD_REQUEST, "操作失败");
        }
    }

}
